package com.example.student_blocket;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Adapter extends BaseAdapter {
	Context context;
	List<AdsListDetails> rowItems;

	public Adapter(Context context, List<AdsListDetails> items) {
		this.context = context;
		this.rowItems = items;
	}

	private class ViewHolder {
		ImageView imageView;
		TextView details;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		LayoutInflater mInflater = (LayoutInflater)
			context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_view, null);
			holder = new ViewHolder();
			holder.details = (TextView) convertView.findViewById(R.id.itemDetails);
			holder.imageView = (ImageView) convertView.findViewById(R.id.image);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}

		AdsListDetails rowItem = (AdsListDetails) getItem(position);
		holder.details.setText(" "+rowItem.getTitle()+"  "+rowItem.getPrice()+"\n\n "+rowItem.getCategory()+"  "+rowItem.getDate());
		holder.imageView.setImageBitmap(rowItem.getImageUri());

		return convertView;
	}

	@Override
	public int getCount() {
		return rowItems.size();
	}

	@Override
	public Object getItem(int position) {
		return rowItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return rowItems.indexOf(getItem(position));
	}
}